package com.example.springboot.service.impl;

import com.example.springboot.common.PageBean.PageBean;
import com.example.springboot.entity.Role;

import com.example.springboot.entity.User;
import com.example.springboot.mapper.RoleMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    public RoleMapper roleMapper;

    @Autowired
    public UserMapper userMapper;

    @Override
    public PageBean page(int pageNum, int pageSize, String roleName) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //执行查询
        List<Role> list = roleMapper.list(roleName);
        Page p = (Page) list;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult(), (long) pageSize, (long) pageNum);
        return pageBean;
    }

    @Override
    public void add(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.add(role);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public void delete(Long id) {
        roleMapper.delete(id);
    }

    @Override
    public List<Role> getAllRole() {
        List<Role> list = roleMapper.listAll();
        return list;
    }

    @Override
    public List<Long> getRoleByUserId(Long id) {
        User user = userMapper.getById(id);
        String AllRoleName = user.getRoleName();
        List<Long> list = roleMapper.getByRoleName(AllRoleName);
        return list;
    }

    @Override
    public List<Role> getRoleByIds(List<Long> roleIdList) {
        if (roleIdList.size() == 0) {
            return null;
        }
        List<Role> list = roleMapper.getByIds(roleIdList);
        return list;
    }

    @Override
    public Role getRoleById(Long id) {
        Role role = roleMapper.getById(id);
        return role;
    }

}
